#!/bin/bash
#2019-06-20 1548
user="`who am i | awk -F" " '{print $1}'`"
newdir="`pwd`/../../bin"
logodir="`pwd`/../../logo"

cd ../os
    source os_info.sh
cd -

if [ "${os_ret}" = "2" ]; then
	if [ "${user}" = "root" ]; then
		path1="/Desktop"
	else
		path1="/export/home/${user}/Desktop"
	fi
else
	if [ "${user}" = "root" ]; then
		path1="/${user}/桌面"
		path2="/${user}/Desktop"
	else
		path1="/home/${user}/桌面"
		path2="/home/${user}/Desktop"
	fi
fi

function f_desktop(){
	if [ ! -f ${src_file_dir} ]; then
		echo "Error:not find ${src_file_dir} stop install"
		exit
	fi

	cp -f ${src_file_dir} ${obj_file_dir}		#创建新启动脚本
	chown -R ${user}:${user} ${obj_file_dir}
	chmod -R 755 ${obj_file_dir}
	sed -i "s:#*cd.*$:cd ${newdir}:g"  ${obj_file_dir}		#修改启动脚本路径
	sed -i "s:./Phoenix.*$:${newcmd}:g" ${obj_file_dir}		#修改启动脚本启动参数

	if [ -d ${path1} ]; then
		obj_desktopfile="${path1}/${desktopfile}"
		echo ${obj_desktopfile}
	elif [ -d ${path2} ]; then
		obj_desktopfile="${path2}/${desktopfile}"
		echo ${obj_desktopfile}
	else
		"Error:not find ${path1} or ${path2} so stop"
		exit
	fi

	cp -f ./autostart/${desktopfile} ${obj_desktopfile}		#创建桌面快捷方式
	chown -R ${user}:${user} ${obj_desktopfile}
	chmod -R 755 ${obj_desktopfile}

	sed -i "s:Exec=.*$:Exec=${obj_file_dir}:g" ${obj_desktopfile}
	sed -i "s:Name\[zh\_CN\]=.*$:Name\[zh\_CN\]=${title_name_cn}:g" ${obj_desktopfile}
	sed -i "s:Name=.*$:Name=${title_name}:g" ${obj_desktopfile}
	sed -i "s:Comment\[zh\_CN\]=.*$:Comment\[zh\_CN\]=${title_name_cn}:g" ${obj_desktopfile}
	sed -i "s:Comment=.*$:Comment=${title_name}:g" ${obj_desktopfile}
	sed -i "s:Comment=.*$:Comment=${title_name}:g" ${obj_desktopfile}
	sed -i "s:Icon=.*$:Icon=${icon_name}:g" ${obj_desktopfile}

}

case "$1" in
	desktop)
		src_file_dir="${newdir}/Phoenix.sh"
		obj_file_dir="${newdir}/.Phoenix_autostart.sh"
		newcmd="./Phoenix"
		icon_name="${logodir}/Phoenix_logo.png"
		desktopfile="Phoenix.desktop"
		title_name=Phoenix
		title_name_cn=电力安全防误管理系统
		f_desktop
	;;
	autoLogin)
		src_file_dir="${newdir}/Phoenix.sh"
		obj_file_dir="${newdir}/.Phoenix_autostart.sh"
		newcmd="./Phoenix autoLoginUser,1"
		icon_name="${logodir}/Phoenix_logo.png"
		desktopfile="Phoenix.desktop"
		title_name=Phoenix
		title_name_cn=电力安全防误管理系统
		f_desktop
	;;
	JKServer)
		src_file_dir="${newdir}/PSM70Server.sh"
		obj_file_dir="${newdir}/.PSM70Server_autostart.sh"
		newcmd="./Phoenix"
		icon_name="${logodir}/PSM70Server_logo.png"
		desktopfile="PSM70Server.desktop"
		title_name=Phoenix
		title_name_cn=电力安全防误管理系统服务端
		f_desktop
	;;
	*)
		echo "Please use autostart or autoLogin or JKServer as first argument"
	;;
esac
